<?php

namespace app\store\controller\order;

use app\store\controller\Controller;
use app\store\model\appointment\Order as AppointmentModel;
use app\store\model\Express as ExpressModel;
use app\store\model\Order as OrderModel;
use app\store\model\OrderRefund as OrderRefundModel;
use app\store\model\ReturnAddress as ReturnAddressModel;
use app\store\model\server\Withdraw as WithdrawModel;
use app\store\model\store\shop\Clerk as ShopClerkModel;
use app\store\model\User as UserModel;

/**
 * 售后管理
 * Class Refund
 * @package app\store\controller\order
 */
class Refund extends Controller
{
    /**
     * 帮助中心列表
     * @return mixed
     * @throws \think\exception\DbException
     */
    public function index($title = '退款', $dataType = 'refund')
    {

        // 订单列表
        $model = new OrderModel;

        $list = $model->getList('refund', $this->request->param());
        return $this->fetch('index', compact('title', 'dataType', 'list'));


        return $this->fetch('index', compact('list'));


    }

    /**
     * 售后单详情
     * @param $order_refund_id
     * @return mixed
     * @throws \think\exception\DbException
     */
    public function detail($order_refund_id)
    {
        // 售后单详情
        $detail = OrderRefundModel::detail($order_refund_id);
        // 订单详情
        $order = OrderModel::detail($detail['order_id']);
        // 退货地址
        $address = (new ReturnAddressModel)->getAll();
        return $this->fetch('detail', compact('detail', 'order', 'address'));
    }

    /**
     * 订单详情
     * @param $order_id
     * @return mixed
     * @throws \think\exception\DbException
     */
    public function refund($order_id)
    {
        // 订单详情
        $detail = OrderModel::detail($order_id);
        // 物流公司列表
        $expressList = ExpressModel::getAll();
        // 门店店员列表
        $shopClerkList = (new ShopClerkModel)->getList(true);
        //服务信息列表
        $appointment = (new AppointmentModel)->orderOrderList($order_id);

        $data = $this->fetch('refund', compact(
            'detail',
            'expressList',
            'shopClerkList',
            'appointment'
        ));

        return $data;
    }

    /**
     * 分销商提现：微信支付企业付款
     * @param $id
     * @return array|bool
     * @throws \app\common\exception\BaseException
     * @throws \think\Exception
     * @throws \think\exception\DbException
     */
    public function pass($order_id = [])
    {

        if(empty($order_id['order_id']) || empty($order_id['money'])){
            return $this->renderError('请先完善表单');
        }

        $model = new OrderModel;

        $order_info = $model::get($order_id['order_id']);

        $appointment = new AppointmentModel;
        if(!$order_info){
            return $this->renderError('订单信息有误');
        }
        if($order_info['pay_type']['value'] == 10){

            $user_model = UserModel::detail($order_info['user_id']);

            if(!$user_model){
                return $this->renderError('订单信息有误');
            }

            if ($user_model->recharge($this->store['user']['user_name'],0,['balance'=>['mode'=>'inc','money'=>$order_id['money'],'remark'=>'订单退款']])) {
                // 确认已打款
                $order_info->is_refund = 20;
                $order_info->refund_money = $order_id['money'];
                $order_info->order_status = 20;
                $order_info->pay_status = 10;

                $order_info->save();



                $appointment->makeRefund($order_info['order_id']);

                return $this->renderSuccess('退款成功');
            }
            return $this->renderError('退款失败');
        }else{
            if ($model->wechatPay($order_id['order_id'],$order_id['money'])) {

                $appointment->makeRefund($order_info['order_id']);

                return $this->renderSuccess('操作成功');
            }
            return $this->renderError($model->getError() ?: '操作失败');
        }

    }

    public function over($order_id = [])
    {
        if(empty($order_id['order_id']) || empty($order_id['remark'])){
            return $this->renderError('请先完善表单');
        }

        $model = OrderModel::detail($order_id['order_id']);

        if(!$model){
            return $this->renderError($model->getError() ?: '操作失败');
        }
        $model->refund_remark = $order_id['remark'];
        $model->is_refund = 30;
        if ($model->save()) {
            return $this->renderSuccess('操作成功');
        }
        return $this->renderError($model->getError() ?: '操作失败');
    }

    /**
     * 商家审核
     * @param $order_refund_id
     * @return array|bool
     * @throws \think\exception\DbException
     */
    public function audit($order_refund_id)
    {
        if (!$this->request->isAjax()) {
            return false;
        }
        $model = OrderRefundModel::detail($order_refund_id);
        if ($model->audit($this->postData('refund'))) {
            return $this->renderSuccess('操作成功');
        }
        return $this->renderError($model->getError() ?: '操作失败');
    }

    /**
     * 确认收货并退款
     * @param $order_refund_id
     * @return array|bool
     * @throws \think\exception\DbException
     */
    public function receipt($order_refund_id)
    {
        if (!$this->request->isAjax()) {
            return false;
        }
        $model = OrderRefundModel::detail($order_refund_id);
        if ($model->receipt($this->postData('refund'))) {
            return $this->renderSuccess('操作成功');
        }
        return $this->renderError($model->getError() ?: '操作失败');
    }

}